/**
 * Created by 55456 on 2016/4/12.
 */
var db = require("../../models/index");
var codeConfig = require("../../configs/codeConfig");
var appConfig  = require("../../configs/appConfig");
var auth = require("../../helpers/auth");
var msgAdaptor = require("../../modelsAdaptor/msgAdaptor");

var autoPrefix = appConfig.autoPrefix;

module.exports = (router) => {
    var User = db.models.User;
    router.post(autoPrefix("chat/send"),function *(){
        this.checkBody("revID").notEmpty();
        this.checkBody("content").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
        }
        var body = this.request.body;
        var sendID = (yield auth.user(this)).id;
        var data = yield msgAdaptor.addMsg(sendID,body.revID,body.content);
        this.body = codeConfig.parseMsg(data);
    });

    router.post(autoPrefix("chat/get"),function *(){
        this.checkBody("revID").notEmpty();
        if(this.errors){
            this.body = codeConfig.parseErrorMsg(this.errors);
        }
        var body = this.request.body;
        var sendID = (yield auth.user(this)).id;
        var data = yield msgAdaptor.getAllMsg(sendID,body.revID);
        this.body = codeConfig.parseMsg(data);
    });
};